#include <queue>
#include <iostream>
using namespace std;

priority_queue<int, vector<int>, greater<int> > Q;

int main () {
	int n;
	while (cin >> n && n) {
		int x;
		for (int i = 0 ; i < n ; i++) {
			cin >> x;
			Q.push(x);
		}

		int a, b, c;
		int sum = 0;
		while (1) {
			a = Q.top();

			Q.pop();
			if (Q.empty())
				break;

			b = Q.top();
			Q.pop();

			c = a + b;

			Q.push(c);
			sum += c;
		}

		cout << sum << endl;
	}
}